package com.hbgc.controller;

import com.alibaba.druid.sql.PagerUtils;
import com.github.pagehelper.PageInfo;
import com.hbgc.common.Combox;
import com.hbgc.common.PageResult;
import com.hbgc.pojo.TbSpider;
import com.hbgc.pojo.TbVideo;
import com.hbgc.service.ISpiderService;
import com.hbgc.utils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/spider")
public class SpiderController {


    @Autowired
    private ISpiderService iSpiderService;

    /**
     * 分页查询
     * @param page
     * @param limit
     * @param spidername
     * @return
     */
    @RequestMapping("/queryByPage")
    @ResponseBody
    public PageResult queryByPage(@RequestParam(required = false,defaultValue = "1") int page, @RequestParam(required = false,defaultValue = "10") int limit,  String spidername)
    {
        PageInfo pageInfo=iSpiderService.queryTbSpiderByPage(page, limit, spidername);
        return ConvertUtils.convertData(pageInfo);
    }


    /**
     * 查询所有
     * @return
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public PageResult queryAll()
    {
        PageResult result=new PageResult(0,"查询成功");
        List<TbSpider> tbSpiders = iSpiderService.queryAll();
        List comboxList=new ArrayList();
        for (TbSpider tbSpider:tbSpiders)
        {
            Combox combox=new Combox();
            combox.setId(tbSpider.getSpiderid());
            combox.setTitle(tbSpider.getSpidername());
            comboxList.add(combox);
        }
        result.setData(comboxList);
        return result;
    }


    /**
     * 保存数据
     * @param tbSpider
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public  PageResult save(TbSpider tbSpider)
    {

        boolean status=iSpiderService.saveTbSpider(tbSpider);
        if(status)
        {
            return new PageResult(0,"保存成功");
        }
        else {
            return new PageResult(1,"保存失败");
        }
    }


    /**
     * 修改数据
     * @param tbSpider
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public  PageResult update(TbSpider tbSpider)
    {

        boolean status=iSpiderService.updateTbSpiderById(tbSpider);
        if(status)
        {
            return new PageResult(0,"修改成功");
        }
        else {
            return new PageResult(1,"修改失败");
        }
    }



    /**
     * 根据主键删除爬虫
     * @param spiderid
     * @return
     */
    @RequestMapping("/remove/{spiderid}")
    @ResponseBody
    public  PageResult remove(@PathVariable Long spiderid)
    {

        boolean status=iSpiderService.deleteTbSpiderById(spiderid);
        if(status)
        {
            return new PageResult(0,"删除成功");
        }
        else {
            return new PageResult(1,"删除失败");
        }
    }

    /**
     * 根据主键查询视频
     * @param spiderid
     * @return
     */
    @RequestMapping("/querybyid/{spiderid}")
    @ResponseBody
    public  PageResult querybyid(@PathVariable Long spiderid)
    {
        PageResult pageResult= new PageResult(0,"查询成功");
        TbSpider tbSpider =iSpiderService.getTbSpiderById(spiderid);
        if(tbSpider!=null)
        {
            List data=new ArrayList<>();
            data.add(tbSpider);
            pageResult.setData(data);
        }
        else {
            pageResult.setCode(1);
            pageResult.setMsg("查询失败");
        }
        return pageResult;
    }



}
